import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../../service/api_method.dart';
import '../../../model/category.dart';
import 'dart:convert';
import 'package:provide/provide.dart';
import '../../../provider/child_category.dart';
import '../../../provider/category_goods.dart';
import '../../../model/category_goods.dart';

class LeftCategory extends StatefulWidget {
  LeftCategory({Key key}) : super(key: key);

  @override
  _LeftCategoryState createState() => _LeftCategoryState();
}

class _LeftCategoryState extends State<LeftCategory> {
  List<BigCategory> categoires = [];
  var _currentIndex = 0;

  @override
  void initState() {
    super.initState();

    // 查询分类数据
    _getCategoriesData();

    // 查询分类下面的商品数据
    _getCategoriesGoodsData();
  }

  _getCategoriesData() async {
    await request('getCategory').then((val) {
      var category = Category.fromJson(json.decode(val));

      setState(() {
        categoires = category.data;
      });

      Provide.value<ChildCategory>(context).setChildCategory(
          category.data[0].bxMallSubDto, category.data[0].mallCategoryId);
    });
  }

  _getCategoriesGoodsData({String categoryId}) async {
    var params = {
      'categoryId':
          categoryId == null ? '2c9f6c946cd22d7b016cd74220b70040' : categoryId,
      'categorySubId': '',
      'page': 1
    };

    await request('getMallGoods', params: params).then((val) {
      CategoryGoods categoryGoods = CategoryGoods.fromJson(json.decode(val));

      // 保存到Provide中
      Provide.value<CategoryGoodsProvide>(context)
          .setCategoryGoodsList(categoryGoods.data);
    });
  }

  Widget _buildItem(context, index) {
    bool isSelect = _currentIndex == index;
    return Container(
      height: ScreenUtil().setHeight(100),
      padding: EdgeInsets.only(left: 10.0, top: 10.0),
      decoration: BoxDecoration(
          color: isSelect ? Color.fromRGBO(236, 238, 239, 1.0) : Colors.white,
          border:
              Border(bottom: BorderSide(width: 1.0, color: Colors.black12))),
      child: InkWell(
        onTap: () {
          setState(() {
            _currentIndex = index;
          });

          var subCategories = categoires[index].bxMallSubDto;

          // 设置值及一级分类id
          Provide.value<ChildCategory>(context).setChildCategory(
              subCategories, categoires[index].mallCategoryId);

          // 查询分类下的商品数据
          _getCategoriesGoodsData(categoryId: categoires[index].mallCategoryId);
        },
        child: Text(categoires[index].mallCategoryName,
            style: TextStyle(fontSize: ScreenUtil().setSp(28))),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(180),
      decoration: BoxDecoration(
          color: Colors.white,
          border: Border(right: BorderSide(width: 1, color: Colors.black12))),
      child: ListView.builder(
        itemCount: categoires.length,
        itemBuilder: (context, index) {
          return _buildItem(context, index);
        },
      ),
    );
  }
}
